using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using DIS_DAL_WS.Models;

namespace DIS_DAL_WS.DTO
{
    [DataContract]
	public class ItemDTO
	{
        [DataMember]
		public int ID { get; set; }
        [DataMember]
		public int IDCategory { get; set; }
        [DataMember]
		public string Name { get; set; }
        [DataMember]
		public double Price { get; set; }
        [DataMember]
        public string Image { get; set; }
        [DataMember]
        public string ContentType { get; set; }
        [DataMember]
        public CategoryDTO Category { get; set; }

        public static ItemDTO Translate(Item Entity)
        {
            if (Entity != null)
            {
                var dto = new ItemDTO
                {
                    ID = Entity.ID,               
                    Name = Entity.Name,
                    Price = Entity.Price == null ? 0 : Entity.Price.Value,
                    Image = Entity.Image,
                    ContentType = Entity.ContentType,
                    Category = CategoryDTO.TranslateBase(Entity.Category)
                };
                if (Entity.IDCategory != null)
                {
                    dto.IDCategory = Entity.IDCategory.Value;
                }
                return dto;
            }
            return null;
        }

        public static Item Reverse(ItemDTO obj)
        {
            var entity = new Item
            {
                ID = obj.ID,
                Name = obj.Name,
                Price = obj.Price,
                Image = obj.Image,
                ContentType = obj.ContentType,
            };
            if (obj.IDCategory != -1)
            {
                entity.IDCategory = obj.IDCategory;
            }
            return entity;
        }

        public void UpdateEntity(ref Item entity)
        {
            entity.Name = Name;
            entity.Price = Price;
            entity.Image = Image;
            entity.ContentType = ContentType;
            if (IDCategory != -1)
            {
                entity.IDCategory = IDCategory;
            }
        }
	}
}